#pragma once
#include <string>
#include <cstddef> 
#include <odb/nullable.hxx>
#include <odb/core.hxx>
#include "chat_session_member.hxx"

// odb -d mysql --std c++11 --generate-query --generate-schema --profile boost/date-time chat_session.hxx
namespace xchat
{
enum class ChatSessionType
{
    SINGLE = 1,
    GROUP = 2
};
#pragma db object table("chat_session")
class ChatSession
{
public:
    ChatSession() {}
    //用户名--新增id -- 用户ID 昵称 密码
    ChatSession(const std::string &chat_session_id, const std::string &chat_session_name, ChatSessionType chat_session_type) 
    : _chat_session_id(chat_session_id)
    , _chat_session_name(chat_session_name)
    , _chat_session_type(chat_session_type)
    {}
    
    std::string chat_session_id() const { return _chat_session_id; }
    void chat_session_id(const std::string &val)  { _chat_session_id = val; }

    std::string chat_session_name() const { return _chat_session_name; }
    void chat_session_name(const std::string &val)  { _chat_session_name = val; }

    ChatSessionType chat_session_type() const { return _chat_session_type; }
    void chat_session_type(ChatSessionType val)  { _chat_session_type = val; }

private:
    friend class odb::access;

    #pragma db id auto
    unsigned long _id;
    #pragma db type("varchar(64)") index unique
    std::string _chat_session_id;
    #pragma db type("varchar(64)") 
    std::string _chat_session_name;
    #pragma db type("tinyint(1)") 
    ChatSessionType _chat_session_type; // 1:单聊 2:群聊

};

//(?)外部调用时传入的过滤条件!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//这里条件必须是指定条件 css::chat_session_type == 1 && csm1.user_id == uid && csm2.user_id != csm1.user_id
#pragma db view object(ChatSession = css)\
                object(ChatSessionMember = csm1 : css::_chat_session_id == csm1::_session_id)\
                object(ChatSessionMember = csm2 : css::_chat_session_id == csm2::_session_id)\
                query((?))

struct SingleChatSession
{
    #pragma db column(css::_chat_session_id)
    std::string _chat_session_id;
    #pragma db column(csm2::_user_id)
    std::string _friend_id;
};

//(?)外部调用时传入的过滤条件!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//这里条件必须是指定条件 css::chat_session_type == 2 && csm1.user_id == uid -->只有一个对象 上面同理
#pragma db view object(ChatSession = css)\
                object(ChatSessionMember = csm : css::_chat_session_id == csm::_session_id)\
                query((?))

struct GroupChatSession
{
    #pragma db column(css::_chat_session_id)
    std::string _chat_session_id;
    #pragma db column(css::_chat_session_name)
    std::string _chat_session_name;
};

// //(?)外部调用时传入的过滤条件!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// //这里条件必须是指定条件 css::chat_session_type == 1 && csm1.user_id == uid && csm2.user_id == pid
// #pragma db view object(ChatSession = css)\
//                 object(ChatSessionMember = csm1 : css::_chat_session_id == csm1::_session_id)\
//                 object(ChatSessionMember = csm2 : css::_chat_session_id == csm2::_session_id)\
//                 query((?))

// struct SingleChatSessionID
// {
//     #pragma db column(css::_chat_session_id)
//     std::string _chat_session_id;
// };

} // namespace xchat